package com.net.xpay.common.service.impl;

import com.google.common.base.Strings;
import com.net.xpay.common.domain.SafePayExcelTemplate;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.manager.SafePayExcelTemplateManager;
import com.net.xpay.common.model.CreateSafePayExcelTemplateModel;
import com.net.xpay.common.model.ModifySafePayExcelTemplateModel;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.model.SafePayExcelTemplateSearchModel;
import com.net.common.exception.BusinessException;
import com.net.xpay.common.service.SafePayExcelTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SafePayExcelTemplateServiceImpl implements SafePayExcelTemplateService {

    @Autowired
    private SafePayExcelTemplateManager safePayExcelTemplateManager;

    @Override
    public SafePayExcelTemplate addSafePayExcelTemplate(User user, CreateSafePayExcelTemplateModel model) {
        if (model.getName() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入模板名称");
        }
        model.setName(model.getName().trim());
        if (Strings.isNullOrEmpty(model.getName())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入模板名称");
        }
        if (model.getName().length() > 8) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "模板名称不能超过8个字");
        }
        if (!Strings.isNullOrEmpty(model.getRemark()) && model.getRemark().length() > 128) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "备注不能超过128个字");
        }

        if (model.getIdentityColumnIndex() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择收款账号在第几列");
        }
        if (model.getNameColumnIndex() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择收款姓名在第几列");
        }
        if (model.getAmountColumnIndex() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择收款金额在第几列");
        }
        SafePayExcelTemplateSearchModel searchModel = new SafePayExcelTemplateSearchModel();
        searchModel.setUserId(user.getId());
        long count = safePayExcelTemplateManager.searchCount(searchModel);
        if (count >= 10L) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "最多创建10个模板");
        }

        SafePayExcelTemplate select = safePayExcelTemplateManager.findByName(user.getId(), model.getName());
        if (select != null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "模板名称已被使用，请使用其他模板名称");
        }

        SafePayExcelTemplate safePayExcelTemplate = new SafePayExcelTemplate();
        safePayExcelTemplate.setUserId(user.getId());
        safePayExcelTemplate.setName(model.getName());
        safePayExcelTemplate.setRemark(model.getRemark());
        safePayExcelTemplate.setType(model.getType());
        safePayExcelTemplate.setIdentityColumnIndex(model.getIdentityColumnIndex());
        safePayExcelTemplate.setNameColumnIndex(model.getNameColumnIndex());
        safePayExcelTemplate.setAmountColumnIndex(model.getAmountColumnIndex());
        safePayExcelTemplateManager.save(safePayExcelTemplate);

        return safePayExcelTemplate;
    }

    @Override
    public void modifySafePayExcelTemplate(User user, ModifySafePayExcelTemplateModel model) {
        if (model.getName() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入模板名称");
        }
        model.setName(model.getName().trim());
        if (Strings.isNullOrEmpty(model.getName())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入模板名称");
        }
        if (model.getName().length() > 16) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "模板名称不能超过16个字");
        }
        if (!Strings.isNullOrEmpty(model.getRemark()) && model.getRemark().length() > 128) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "备注不能超过128个字");
        }

        if (model.getIdentityColumnIndex() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择收款账号在第几列");
        }
        if (model.getNameColumnIndex() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择收款姓名在第几列");
        }
        if (model.getAmountColumnIndex() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择收款金额在第几列");
        }

        SafePayExcelTemplate select = safePayExcelTemplateManager.findByName(user.getId(), model.getName());
        if (select != null && !select.getId().equals(model.getId())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "模板名称已被使用，请使用其他模板名称");
        }
        select = safePayExcelTemplateManager.findById(user.getId(), model.getId());
        select.setName(model.getName());
        select.setRemark(model.getRemark());
        select.setIdentityColumnIndex(model.getIdentityColumnIndex());
        select.setNameColumnIndex(model.getNameColumnIndex());
        select.setAmountColumnIndex(model.getAmountColumnIndex());
        safePayExcelTemplateManager.update(select);
    }


}
